export const windowDrag = {
    mounted (el) 
    {
        let x = 0;
        let y = 0;
        let l = 0;
        let t = 0;
        let isDown = false;
        const minWidth = 260;
        const minHeight = 300;
        let isMove = true;

        if (el.getAttribute('data-flag') === '0')
        {
            el.children[1].onmousedown = function (e) 
            {
                e.stopPropagation();
            };
    
            el.children[0].children[0].children[1].onmousedown = function (e) 
            {
                e.stopPropagation();
            };
            el.children[0].children[1].children[0].onmousedown = function (e) 
            {
                e.stopPropagation();
            };
        } 
        else
        {
            el.children[0].children[1].onmousedown = function (e) 
            {
                e.stopPropagation();
            };
            el.nextElementSibling.children[0].children[0].onmousedown = function (event)
            {
                event.stopPropagation();
            };
        }

        // 鼠标按下事件
        el.onmousedown = function (e) 
        {
            console.log(el);
            // 获取鼠标位置的x坐标和y坐标
            x = e.pageX;
            y = e.pageY;

            // 获取选中元素左部和顶部的偏移量
            l = el.parentNode.offsetLeft;
            t = el.parentNode.offsetTop;
            // 开关打开
            isDown = true;
            // 设置样式
            //   el.style.cursor = "move";
            el.parentNode.style.zIndex = 2001;
            
            document.addEventListener('mousemove', move);
            document.addEventListener('mouseup', up);
            // 鼠标移动事件
            function move (e) 
            {
                if (isDown === false) 
                {
                    return;
                }
                if (el.parentNode.offsetWidth >= document.body.offsetWidth && el.parentNode.offsetHeight >= document.body.offsetHeight - 80)
                {
                    if (x !== e.pageX || y !== e.pageY)
                    {
                        el.parentNode.style.width = document.body.offsetWidth - 100 + 'px';
                        el.parentNode.style.height = document.body.offsetHeight - 100 + 'px';
                    }
                    
                }
               
                // 获取鼠标每次移动的x和y坐标
                let nx = e.pageX;
                let ny = e.pageY;
                // 计算移动后的左偏移量和顶部的偏移量
                let nl = nx - (x - l);
                let nt = ny - (y - t);
                
                if (nl < -100 || nl > (document.body.offsetWidth - 250) || nt < 0 || nt > (document.body.offsetHeight - 250)) return;
                el.parentNode.style.left = nl + 'px';
                el.parentNode.style.top = nt + 'px';

            }
        
            // 鼠标抬起事件
            function up () 
            {
                // 开关关闭
                isDown = false;
                // el.style.cursor = "pointer";
                
                el.parentNode.style.zIndex = 999;
                // el.parentNode.setAttribute('index', el.parentNode.style.zIndex);
                document.removeEventListener('mousemove', move);
                document.removeEventListener('mouseup', up);
            }
        };
        
        
        el.parentNode.onmousemove = function (e) 
        {
            if 
            (
                e.pageX > el.parentNode.offsetLeft + el.parentNode.offsetWidth - 10 ||
                el.parentNode.offsetLeft + 10 > e.pageX
            ) 
            {
                el.parentNode.style.cursor = 'w-resize';
            } 
            else if 
            ( 
                el.parentNode.scrollTop + e.pageY >
                el.parentNode.offsetTop + el.parentNode.offsetHeight - 10 ||
                el.parentNode.offsetTop + 10 > e.pageY
            ) 
            {
                el.parentNode.style.cursor = 's-resize';
            } 
            else 
            {
                el.parentNode.style.cursor = 'default';
                el.parentNode.onmousedown = null;
            }

            el.parentNode.onmousedown = (e) => 
            {
                const pageX = e.pageX;
                const pageY = e.pageY;
                const elW = el.parentNode.offsetWidth;
                const elH = el.parentNode.offsetHeight;
                const EloffsetLeft = el.parentNode.offsetLeft;
                const EloffsetTop = el.parentNode.offsetTop;
                // el.parentNode.style.userSelect = 'none'
                const ELscrollTop = el.parentNode.scrollTop;

                let elArray = Array.from(el.nextElementSibling.children);
                elArray.forEach((element:any) => 
                {
                    let i = element.children.length - 1;
                    element.children[i].style.display = 'block';
                });
            

                document.onmousemove = function (e) 
                {
                    // 移动时禁用默认事件
                    e.preventDefault();
                    // 左侧鼠标拖拽位置
                    if (pageX > EloffsetLeft && pageX < EloffsetLeft + 10) 
                    {
                        // 往左拖拽
                        if (pageX > e.pageX) 
                        {
                            el.parentNode.style.width = elW + (pageX - e.pageX)  + 'px';
                            el.parentNode.style.left = EloffsetLeft - (pageX - e.pageX)  + 'px';
                        }
                        // 往右拖拽
                        else if (pageX < e.pageX) 
                        {
                            if (el.parentNode.offsetWidth >= minWidth) 
                            {
                                el.parentNode.style.width = elW - (e.pageX - pageX)  + 'px';
                                el.parentNode.style.left = EloffsetLeft + (e.pageX - pageX)  + 'px';
                            }
                        }
                    }
                    // 右侧鼠标拖拽位置
                    if (
                        pageX > EloffsetLeft + elW - 10 &&
                      pageX < EloffsetLeft + elW
                    ) 
                    {
                    // 往左拖拽
                        if (pageX > e.pageX) 
                        {
                            if (el.parentNode.offsetWidth >= minWidth) 
                            {
                                el.parentNode.style.width = elW - (pageX - e.pageX) + 'px';
                            }
                        }
                        // 往右拖拽
                        if (pageX < e.pageX) 
                        {
                            el.parentNode.style.width = elW + (e.pageX - pageX) + 'px';
                        }
                    }

                    // 底部鼠标拖拽位置
                    if (
                        ELscrollTop + pageY > EloffsetTop + elH - 20 &&
                      ELscrollTop + pageY < EloffsetTop + elH
                    ) 
                    {
                        // 往上拖拽
                        if (pageY > e.pageY) 
                        {
                            if (el.parentNode.offsetHeight >= minHeight) 
                            {
                                el.parentNode.style.height = elH - (pageY - e.pageY)  + 'px';
                            }
                        }
                        // 往下拖拽
                        else if (pageY < e.pageY) 
                        {
                            el.parentNode.style.height = elH + (e.pageY - pageY) + 'px';
                        }

                    }
                    // 顶部鼠标拖拽位置
                    if (
                        pageY > EloffsetTop &&
                        pageY < EloffsetTop + 10
                    ) 
                    {
                    // 往下拖拽
                        if (pageY < e.pageY) 
                        {
                            if (el.parentNode.offsetHeight >= minHeight) 
                            {
                                isDown = false;
                                el.parentNode.style.height = elH - (e.pageY - pageY)  + 'px';
                                el.parentNode.style.top = EloffsetTop + (e.pageY - pageY)  + 'px';
                            }
                        }
                        // 往上拖拽
                        else if (pageY > e.pageY) 
                        {
                            isDown = false;
                            el.parentNode.style.height = elH + (pageY - e.pageY) + 'px';
                            el.parentNode.style.top = EloffsetTop - (pageY - e.pageY)  + 'px';
                        }
                    }
                };
            
                // 拉伸结束
                document.onmouseup = function () 
                {
                    document.onmousemove = null;
                    document.onmouseup = null;
                    isDown = true;
                    let elArrays = Array.from(el.nextElementSibling.children);
                    elArrays.forEach((element:any) => 
                    {
                        let i = element.children.length - 1;
                        element.children[i].style.display = 'none';
                    });
                };
            };
        };
    }
};